package com.dzl.middleground.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dzl.brand.pojo.WelfareBrand;
import com.dzl.brand.service.WelfareBrandService;
import com.dzl.goods.service.WelfareGoodsClassifyService;
import com.dzl.goods.service.WelfareGoodsService;
import com.dzl.middleground.pojo.query.SearchGoodsQuery;
import com.dzl.middleground.pojo.vo.SearchGoodsListVo;
import com.dzl.middleground.service.MiddlegroundService;
import com.dzl.partition.pojo.WelfarePartition;
import com.dzl.partition.service.WelfarePartitionService;
import com.dzl.supplier.pojo.WelfareSupplier;
import com.dzl.supplier.service.WelfareSupplierService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.ImageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;

/**
 * @Description:中台项目Api-接口实现
 * @author liufan
 * @time:2021年6月19日 下午4:47:37
 */
@Service
public class MiddlegroundServiceImpl implements MiddlegroundService{

	@Autowired
	private WelfareGoodsService goodsService;
	@Autowired
	private WelfareGoodsClassifyService welfareGoodsClassifyService;
	@Autowired
	private WelfareBrandService welfareBrandService;
	@Autowired
	private WelfarePartitionService welfarePartitionService;
	@Autowired
	private WelfareSupplierService welfareSupplierService;

	@Override
	public PageInfo queryGoodsListByMiddleground(SearchGoodsQuery query) {
		PageHelper.startPage(query);
		List<SearchGoodsListVo> list=goodsService.queryGoodsListByMiddleground(query);
		if(EmptyUtils.isNotEmpty(list)) {
			list.forEach(x->{
				x.setGoodsImg(ImageUtils.setImageSrc(x.getGoodsImg()).split(",")[0]);
			});
		}
		return new PageInfo(list);
	}

	@Override
	public Map<String, Object> getQueryCriteria() {
		Map<String, Object> result=new HashMap<>();
		//查询分区
		Example example=Example.builder(WelfarePartition.class).where(Sqls.custom()
				.andEqualTo("status", 1))
				.orderByAsc("createTime").build();
		List<WelfarePartition> partitionList=welfarePartitionService.selectByExample(example);
		List<Map<String, Object>> partitionMapList=null;
		if(EmptyUtils.isNotEmpty(partitionList)) {
			partitionMapList=partitionList.stream().map(x->{
				Map<String, Object> map=new HashMap<String,Object>();
				map.put("id", x.getId());
				map.put("name", x.getPartitionName());
				return map;
			}).collect(Collectors.toList());
		}
		//查询品牌
		example=Example.builder(WelfareBrand.class).where(Sqls.custom()
				.andEqualTo("status",1 )).orderByAsc("sort").build();
		List<WelfareBrand> brandList=welfareBrandService.selectByExample(example);
		List<Map<String, Object>> brandMapList=null;
		if(EmptyUtils.isNotEmpty(brandList)) {
			brandMapList=brandList.stream().map(x->{
				Map<String, Object> map=new HashMap<>();
				map.put("id", x.getId());
				map.put("name", x.getName());
				return map;
			}).collect(Collectors.toList());
		}
		//查询供应商
		example=Example.builder(WelfareSupplier.class).where(Sqls.custom()
				.andEqualTo("status",1 )).orderByAsc("createTime").build();
		List<WelfareSupplier> businessList=welfareSupplierService.selectByExample(example);
		List<Map<String, Object>> businessMapList=null;
		if(EmptyUtils.isNotEmpty(brandList)) {
			businessMapList=businessList.stream().map(x->{
				Map<String, Object> map=new HashMap<>();
				map.put("id", x.getId());
				map.put("name", x.getName());
				return map;
			}).collect(Collectors.toList());
		}
		result.put("brandList", brandMapList);
		result.put("businessList", businessMapList);
		result.put("partitionList", partitionMapList);
		return result;
	}

	@Override
	public List<Map<String, Object>> getGoodsCategoryList(Integer parentId) {
		List<Map<String, Object>> goodsCategoryMapList=welfareGoodsClassifyService.getCategoryListByMiddleground(parentId);
		if(EmptyUtils.isNotEmpty(goodsCategoryMapList)) {
			goodsCategoryMapList=goodsCategoryMapList.stream().filter(x->((Integer)x.get("level")!=3&&(Long)x.get("childrenCount")!=0)||(Integer)x.get("level")==3).collect(Collectors.toList());
		}
		return goodsCategoryMapList;
	}
}
